package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TicketBusiness;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITicketBusinessService;
import com.ruoyi.system.service.TicketCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TicketTemplateMapper;
import com.ruoyi.system.domain.TicketTemplate;
import com.ruoyi.system.service.ITicketTemplateService;

import javax.validation.constraints.Size;

/**
 * 收据模板信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-15
 */
@Service
public class TicketTemplateServiceImpl implements ITicketTemplateService
{
    @Autowired
    private TicketTemplateMapper ticketTemplateMapper;


    @Autowired
    private ISysUserService userService;

    @Autowired
    private TicketCommonService commonService;

    @Autowired
    private ITicketBusinessService businessService;

    /**
     * 查询收据模板信息
     *
     * @param templateNo 收据模板信息主键
     * @return 收据模板信息
     */
    @Override
    public TicketTemplate selectTicketTemplateByTemplateNo(String templateNo)
    {
        return ticketTemplateMapper.selectTicketTemplateByTemplateNo(templateNo);
    }

    /**
     * 查询收据模板信息列表
     *
     * @param ticketTemplate 收据模板信息
     * @return 收据模板信息
     */
    @Override
    public List<TicketTemplate> selectTicketTemplateList(TicketTemplate ticketTemplate)
    {
        List<TicketTemplate> ticketTemplates = ticketTemplateMapper.selectTicketTemplateList(ticketTemplate);
        //判断当前登录人是否是商户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        SysUser sysUser = userService.selectUserById(userId);
        @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符") String nickName = sysUser.getNickName();
        if(commonService.isBusiness()){
            TicketBusiness business = businessService.selectTicketBusinessByBusinessName(nickName);
            String businessPhone = business.getBusinessPhone();
            ticketTemplates= ticketTemplates
                    .stream()
                    .filter(p->p.getCreateBy().equals(businessPhone))
                    .collect(Collectors.toList());
            //判断是否是用户
        }
        return ticketTemplates;
    }

    /**
     * 新增收据模板信息
     *
     * @param ticketTemplate 收据模板信息
     * @return 结果
     */
    @Override
    public int insertTicketTemplate(TicketTemplate ticketTemplate)
    {
        if(StringUtils.isEmpty(ticketTemplate.getTemplateNo())){
            ticketTemplate.setTemplateNo(UUID.randomUUID().toString());
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ticketTemplate.setCreateBy(loginUser.getUsername());
        ticketTemplate.setCreateTime(DateUtils.getNowDate());
        return ticketTemplateMapper.insertTicketTemplate(ticketTemplate);
    }

    /**
     * 修改收据模板信息
     *
     * @param ticketTemplate 收据模板信息
     * @return 结果
     */
    @Override
    public int updateTicketTemplate(TicketTemplate ticketTemplate)
    {
        ticketTemplate.setUpdateTime(DateUtils.getNowDate());
        return ticketTemplateMapper.updateTicketTemplate(ticketTemplate);
    }

    /**
     * 批量删除收据模板信息
     *
     * @param templateNos 需要删除的收据模板信息主键
     * @return 结果
     */
    @Override
    public int deleteTicketTemplateByTemplateNos(String[] templateNos)
    {
        return ticketTemplateMapper.deleteTicketTemplateByTemplateNos(templateNos);
    }

    /**
     * 删除收据模板信息信息
     *
     * @param templateNo 收据模板信息主键
     * @return 结果
     */
    @Override
    public int deleteTicketTemplateByTemplateNo(String templateNo)
    {
        return ticketTemplateMapper.deleteTicketTemplateByTemplateNo(templateNo);
    }
}
